/**
 * @description 文章实体
 * @author LI
 * @date 2020-11-11 16:03
 */

import {
  Column,
  CreateDateColumn,
  Entity, JoinColumn,
  ManyToOne, OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn
} from 'typeorm'
import { UserEntity } from '../user/user.entity'
import { CommentEntity } from '../comment/comment.entity'
import { IsDefined } from 'class-validator'

@Entity('li_articles')
export class ArticleEntity {
  @PrimaryGeneratedColumn({ comment: '主键id' })
  id: number

  @ManyToOne(() => UserEntity, user => user.articles)
  @JoinColumn({ name: 'user_id' })
  userId: UserEntity

  @IsDefined()
  @Column({ type: 'varchar', length: 50, nullable: true, default: '标题', comment: '文章标题' })
  title: string

  @Column({ type: 'longtext', nullable: true, comment: '文章内容' })
  content: string

  @CreateDateColumn()
  @Column({ type: 'datetime', name: 'created_at', comment: '创建时间' })
  createdAt: Date

  @CreateDateColumn()
  @UpdateDateColumn()
  @Column({ type: 'datetime', name: 'updated_at', comment: '更新时间' })
  updatedAt: Date

  @OneToMany(() => CommentEntity, comment => comment.articleId)
  comments: CommentEntity[]
}